package com.foodorder.repository;

import com.foodorder.entity.DeliveryTracking;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
 * 配送跟踪数据访问接口
 */
@Repository
public interface DeliveryTrackingRepository extends JpaRepository<DeliveryTracking, Long> {
    
    /**
     * 根据订单ID查找配送记录
     */
    Optional<DeliveryTracking> findByOrderId(Long orderId);
    
    /**
     * 根据配送员ID和配送状态查找配送记录
     */
    List<DeliveryTracking> findByDeliveryStaffIdAndStatus(Long deliveryStaffId, DeliveryTracking.DeliveryStatus status);
    
    /**
     * 根据配送员ID查找配送记录
     */
    List<DeliveryTracking> findByDeliveryStaffId(Long deliveryStaffId);
    
    /**
     * 查找指定时间段内的配送记录
     */
    @Query("SELECT d FROM DeliveryTracking d WHERE d.createdAt BETWEEN :startTime AND :endTime")
    List<DeliveryTracking> findByTimeRange(@Param("startTime") java.time.LocalDateTime startTime, 
                                          @Param("endTime") java.time.LocalDateTime endTime);
    
    /**
     * 根据配送状态统计配送记录数量
     */
    long countByStatus(DeliveryTracking.DeliveryStatus status);
} 